package com.aaa.cts.controller;

import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.Result;
import com.aaa.cts.entity.UserRole;
import com.aaa.cts.service.UserRoleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户和角色关联表(UserRole)表控制层
 *
 * @author makejava
 * @since 2020-12-02 21:10:48
 */
@RestController
@RequestMapping("userRole")
public class UserRoleController {
    /**
     * 服务对象
     */
    @Resource
    private UserRoleService userRoleService;

    /**
     * 通过主键查询数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ApiOperation("通过主键查询数据")
    public Result selectOne(Integer id) {
        return this.userRoleService.queryById(id);
    }

    /**
     * 查询所有分页加条件查询
     *
     * @param pageNo   第几页
     * @param pageSize 每页显示数量
     * @param userRole 实体
     * @return Result
     */
    @PostMapping("queryAll")
    @ApiOperation("查询所有分页加条件查询")
    public Result queryAll(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestBody UserRole userRole) {
        return this.userRoleService.queryAll(pageNo, pageSize, userRole);
    }


    /**
     * 根据ID删除
     *
     * @param userId id
     * @return Result
     */
    @DeleteMapping("deleteById")
    @ApiOperation("根据ID删除")
    public Result deleteById(Integer userId) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.userRoleService.deleteById(userId));
    }

/*    *//**
     * 更新
     *
     * @param @param userRole 实例对象
     * @return Result
     *//*
    @PutMapping("update")
    @ApiOperation("更新")
    public Result update(@ApiParam("更新json数据") @RequestBody UserRole userRole) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.userRoleService.update(userRole));
    }*/

    /**
     * 添加
     *
     * @param @param userRole 实例对象
     * @return Result
     */
    @PostMapping("insert")
    @ApiOperation("添加")
    public Result insert(@ApiParam("添加json数据") @RequestBody UserRole userRole) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.userRoleService.insert(userRole));
    }

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param entities List<UserRole> 实例对象列表
     * @return 影响行数
     */
    @PostMapping("insertOrUpdateBatch")
    @ApiOperation("批量新增或按主键更新数据")
    public Result insertOrUpdateBatch(@Param("entities") List<UserRole> entities){
        return this.userRoleService.insertOrUpdateBatch(entities);
    }

}